﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using System.Diagnostics;
using MyEngineExercise.我的扩展;
using System.IO;
using System.Text.RegularExpressions;

namespace MyEngineExercise
{
    public partial class MainForm : Form
    {
        //导出多个mxd文档中的指定图层（东源规划系统用到）
        private void 导出多个mxd文档中的指定图层201207ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<string> pathStr = new List<string>();//指定文件的路径

            FolderBrowserDialog FolderBrowserDialog = new FolderBrowserDialog();

            MessageBox.Show("下面开始选择源文件夹路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (FolderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.Default;
                string file = GetSplFloderSplFiles.GetFiles(new DirectoryInfo(FolderBrowserDialog.SelectedPath), "*.mxd");
                this.Cursor = Cursors.WaitCursor;

                string[] strArr = file.Split(';');//注意数组内最后一个为空

                Debug.Print("数组长度：" + strArr.Length.ToString());
                Debug.Print("打印出每个mxd文件路径...开始");
                foreach (var fl in strArr)
                {
                    Debug.Print(fl);
                }
                Debug.Print("打印出每个mxd文件路径...结束");

                Debug.Print("移除最后一项...开始");
                pathStr = strArr.ToList();
                Debug.Print("移除前最后一个的值:" + pathStr[pathStr.Count - 1]);
                pathStr.RemoveAt(pathStr.Count - 1);
                Debug.Print("移除后最后一个的值:" + pathStr[pathStr.Count - 1]);
                Debug.Print("移除最后一项...结束");
            }
            else
                return;

            Debug.Print("过滤不符合条件的路径...开始");
            Regex regex = new Regex(@"^[a-zA-Z0-9]+$");//文件名只能包含字母和数字
            Debug.Print("过滤前的个数:" + pathStr.Count.ToString());
            List<string> filterStr = new List<string>();//过滤不符合条件的路径
            filterStr = pathStr.Where(v => regex.IsMatch(System.IO.Path.GetFileNameWithoutExtension(v)) == true).Select(v => v).ToList();
            Debug.Print("过滤后的个数:" + filterStr.Count.ToString());
            Debug.Print("过滤不符合条件的路径...结束");


            //测试导出图层

            //导出到的文件夹路径
            FolderBrowserDialog FolderBrowserDialog_toFolderPath = new FolderBrowserDialog();

            string toFolderPathStr = string.Empty;//导出到文件夹路径


            MessageBox.Show("下面开始要导出文件夹路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (FolderBrowserDialog_toFolderPath.ShowDialog() == DialogResult.OK)
            {
                toFolderPathStr = FolderBrowserDialog_toFolderPath.SelectedPath;
            }
            else
                return;

            if (toFolderPathStr == string.Empty)
            {
                //Debug.Print("选择的导出到的文件夹路径为空");
                MessageBox.Show("选择的导出到的文件夹路径为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            ////测试
            ////string testStr = filterStr[0];//测试一个路径
            //List<string> testList = new List<string>();
            //testList.Add(filterStr[0]);
            //testList.Add(filterStr[1]);

            int exportCount = 0;//导出次数
            int fileCountTotal = 0;//导出的文件的个数
            List<string> noEnough = new List<string>();//导出不够4个的信息
            int remainCount = filterStr.Count;//剩余的个数

            Debug.Print("开始导出:");
            this.Cursor = Cursors.WaitCursor;
            //foreach (var testStr in testList)
            foreach (var testStr in filterStr)
            {
                //如果不存在或损坏
                if (!System.IO.File.Exists(testStr) || !this.axPageLayoutControl1.CheckMxFile(testStr))
                {
                    MessageBox.Show("无法打开Mxd文档，不存在或损毁。\n" + testStr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                //string sabc = "123";

                int fileCount = 0;


                this.axPageLayoutControl1.LoadMxFile(testStr);

                Debug.Print("开始导出下一个：");
                //导出为lyr文件
                for (int i = 0; i < axPageLayoutControl1.ActiveView.FocusMap.LayerCount; i++)
                {
                    //Debug.Print("图层名称:" + axPageLayoutControl1.ActiveView.FocusMap.get_Layer(i).Name);

                    ILayer layer = axPageLayoutControl1.ActiveView.FocusMap.get_Layer(i);
                    if (layer.Name.Contains("行政区Anno") ||
                        layer.Name.Contains("地类图斑Anno") ||
                        layer.Name.Equals("线状地物") ||
                        layer.Name.Equals("地类界线"))   //只导出这几个
                    {

                        ILayerFile pLayerFile = new LayerFileClass();
                        //pLayerFile.New("F:\\atest\\" + "abc.lyr");


                        string fullName = string.Format("{0}\\{1}_{2}.lyr", toFolderPathStr, System.IO.Path.GetFileNameWithoutExtension(testStr), layer.Name);
                        if (File.Exists(fullName))
                        {
                            try
                            {
                                System.IO.File.Delete(fullName);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("删除jpg文件失败！" + Environment.NewLine + ex.Message, "导出jpg");
                                return;
                            }
                        }
                        //pLayerFile.New(toFolderPathStr + "\\" + System.IO.Path.GetFileNameWithoutExtension(testStr) + "_" + axPageLayoutControl1.ActiveView.FocusMap.get_Layer(i).Name + ".lyr");
                        pLayerFile.New(fullName);
                        pLayerFile.ReplaceContents(layer);
                        pLayerFile.Save();

                        fileCount++;

                    }

                }
                remainCount--;
                Debug.Print(System.IO.Path.GetFileNameWithoutExtension(testStr) + "_导出结束,剩余" + remainCount.ToString() + "个");

                if (fileCount < 4)
                    noEnough.Add(System.IO.Path.GetFileNameWithoutExtension(testStr) + "导出的文件不够4个。总共导出了" + fileCount.ToString() + "个。原因：mxd没有现状地物");

                exportCount++;
                fileCountTotal += fileCount;

            }
            this.Cursor = Cursors.Default;
            Debug.Print("全部导出完成！");

            Debug.Print("导出总次数" + exportCount.ToString());
            Debug.Print("导出总文件数" + fileCountTotal.ToString());
            if (noEnough.Count != 0)
            {
                foreach (var asd in noEnough)
                {
                    Debug.Print(asd);
                }
            }
        }
    }
}
